package com.ekuaitu.kuaitu.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.ekuaitu.kuaitu.R;
import com.ekuaitu.kuaitu.activity.LoginActivity;
import com.ekuaitu.kuaitu.adapter.MyCouponAdapter;
import com.ekuaitu.kuaitu.api.ApiStore;
import com.ekuaitu.kuaitu.api.UrlConstant;
import com.ekuaitu.kuaitu.bean.MyCouponBean;
import com.ekuaitu.kuaitu.utils.MyApplication;
import com.ekuaitu.kuaitu.utils.Toast.ToastCompat;

import java.util.List;

import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyCouponFragment extends Fragment {

    /*@BindView(R.id.recyclerView_myCoupon)
    RecyclerView recyclerViewMyCoupon;
    @BindView(R.id.no_order_myCoupon)
    ImageView noOrderMyCoupon;
    @BindView(R.id.layout_noCoupon)
    RelativeLayout layoutNoCoupon;*/

    private Context mContext;
    private int tabIndex;
    private RecyclerView recyclerViewMyCoupon;
    private RelativeLayout layoutNoCoupon;
    private MyCouponAdapter adapter;
    //private List<MyCouponBean.AttachmentBean.ModelsBean> totallist = new ArrayList<>();
    Call<MyCouponBean> callAllCoupon;

    public MyCouponFragment() {
        // Required empty public constructor
    }

    public static MyCouponFragment newInstance(int tabIndex) {
        MyCouponFragment fragment = new MyCouponFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("tabIndex", tabIndex);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        tabIndex = bundle.getInt("tabIndex");
        mContext = getContext();
        loadNetworkData(tabIndex);
    }

    @Override
    public void onResume() {
        super.onResume();
        loadNetworkData(tabIndex);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_my_coupon, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerViewMyCoupon = (RecyclerView) view.findViewById(R.id.recyclerView_myCoupon);
        layoutNoCoupon = (RelativeLayout) view.findViewById(R.id.layout_noCoupon);
        recyclerViewMyCoupon.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        loadNetworkData(tabIndex);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    public void loadNetworkData(final int tabIndex) {
        if (MyApplication.getInstance().getUid() == null) {
            ToastCompat.makeText(mContext, "请重新登录", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(mContext, LoginActivity.class);
            startActivity(intent);
            return;
        }
        callAllCoupon = ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getAllCoupon(MyApplication.getInstance().getRequestFieldMap(), "1", "160", tabIndex + "");
        callAllCoupon.enqueue(new Callback<MyCouponBean>() {
            @Override
            public void onResponse(Call<MyCouponBean> call, Response<MyCouponBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200 && response.body().getAttachment() != null) {
                        List<MyCouponBean.AttachmentBean.ModelsBean> result = response.body().getAttachment().getModels();
                        if (result.size() == 0) {
                            recyclerViewMyCoupon.setVisibility(View.GONE);
                            layoutNoCoupon.setVisibility(View.VISIBLE);
                        } else {
                            layoutNoCoupon.setVisibility(View.GONE);
                            recyclerViewMyCoupon.setVisibility(View.VISIBLE);
                            adapter = new MyCouponAdapter(mContext, result, tabIndex);
                            recyclerViewMyCoupon.setAdapter(adapter);
                        }
                    } else if (response.body().getStatus() == 400) {
                        recyclerViewMyCoupon.setVisibility(View.GONE);
                        layoutNoCoupon.setVisibility(View.VISIBLE);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                } else {
                    ToastCompat.makeText(mContext, "请重新登录", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(Call<MyCouponBean> call, Throwable t) {
                ToastCompat.makeText(mContext, "网络无法访问，请检查网络连接", Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        callAllCoupon.cancel();
    }
}
